package ai.moran.demo.controller;

import ai.moran.demo.common.annotation.SysLog;
import ai.moran.demo.enums.TurnstileEvents;
import ai.moran.demo.enums.TurnstileStates;
import ai.moran.demo.service.PersonTableService;
import ai.moran.demo.service.impl.PersonTableServiceImpl;
import ai.moran.demo.stateMachine.test.TurnstileStateMachineService;
import ai.moran.demo.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.*;

/**
 * @author zhuhaitao
 * @email zhuhaitao@imoran.net
 * @date 2020/7/30 1:25 下午
 */
@RestController
public class TestController {

    @Autowired
    private PersonTableServiceImpl personTableService;
    @Autowired
    private TurnstileStateMachineService turnstileStateMachineService;


    private static final int CORE_POOL_SIZE = 5;
    private static final int MAX_POOL_SIZE = 10;
    private static final int QUEUE_CAPACITY = 100;
    private static final Long KEEP_ALIVE_TIME = 1L;

    @GetMapping("/test")
    @SysLog("测试状态机")
    public R test(Integer id, String event) {
        return turnstileStateMachineService.sendEvent(id, TurnstileEvents.getEventByString(event));
    }

    @GetMapping("/test1")
    public R test1() {
        //通过ThreadPoolExecutor构造函数自定义参数创建
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                CORE_POOL_SIZE,
                MAX_POOL_SIZE,
                KEEP_ALIVE_TIME,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(QUEUE_CAPACITY),
                new ThreadPoolExecutor.CallerRunsPolicy());

        for (int i = 1; i < 100; i++) {
            //创建WorkerThread对象（WorkerThread类实现了Runnable 接口）
            //执行Runnable
            executor.execute(personTableService);
        }
        //终止线程池
        executor.shutdown();
        return R.ok();

    }
}


